package com.tools.util;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.Properties;

public class PropertiesUtils {

	/**
	 * 获取property文件的属性值默认为config.properties
	 * @param key
	 * @return
	 */
	public static String getValueByProperty(String key){
		return getValueByProperty(key,"/config/config.properties");
	}
	
	/**
	 * 通过文件路径获取property文件的属性值
	 * @param key
	 * @param filePath
	 * @return
	 */
	public static String getValueByProperty(String key,String filePath){
		Properties p = new Properties();
		String defaultPath = "/config/config.properties";
		if(StringUtils.isEmpty(filePath)){
			filePath = defaultPath;
		}
		try {
			//获取classes下的路径
			String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
			File f=new File(path+filePath);
			InputStream in = new FileInputStream(f);
			p.load(in);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String path = p.getProperty(key);
		return path;
	}
}
